import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../views/Home.vue'
import Welcome from  '../views/Welcome.vue'
import User from  '../views/user/User.vue'
import Rights from '../views/power/rights.vue'
import Roles from  '../views/power/roles.vue'
import Cate from  '../views/goods/cate.vue'

Vue.use(VueRouter)
  const routes = [
    {path:'/',redirect:'/login'},
    {path: '/login', name: '登录', component: Login},
    {path: '/home', name: '首页', component: Home, redirect:'/welcome',
      children:[
        {path: '/welcome', component: Welcome},
        {path: '/users', name: '用户列表', component: User},
        {path: '/roles', name: '角色列表', component: Roles},
        {path: '/rights', name: '权限列表', component: Rights},
        {path: '/categories', name: '商品分类', component: Cate}
      ]
    }
]

const router = new VueRouter({
  model:'history',
  routes
})

// 挂载路由导航守卫，用于判断登录状态页面的跳转
router.beforeEach(((to, from, next) => {
//  to 将要访问的路径
//  from从哪个路径跳转过来
//  next是一个函数，表示放行
//     next放行 next('login') 强制跳转
    if (to.path === '/login') return next();
//    获取token
    const tokenStr = window.sessionStorage.getItem('token');
    if (!tokenStr) return next('/login')
    next()
  
}))


export default router
